package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.framework.web.domain.server.Sys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 预约信息Service业务层处理
 * 
 * @author alexis
 * @date 2025-01-09
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return reservationMapper.selectById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return reservationMapper.insert(reservation);
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return reservationMapper.updateById(reservation);
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return reservationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return reservationMapper.deleteById(id);
    }

    /**
     * 获取当天取消预约次数
     *
     * @return
     */
    @Override
    public Integer getCanaelledCount() {
        // 获取当前时间的毫秒数
        Long time = System.currentTimeMillis();
        // 将毫秒数转换为LocalDateTime对象
        LocalDateTime lat= LocalDateTimeUtil.of(time);
        // 获取当天开始时间
        LocalDateTime startTime=lat.toLocalDate().atStartOfDay();
        // 获取当天结束时间
        LocalDateTime endTime=startTime.plusDays(1);
        //在线程中获取UserId
        Long userId = UserThreadLocal.getUserId();
        if(ObjectUtil.isEmpty(userId)){
            return null;
        }
        //查询当天取消预约数量
        Integer num=reservationMapper.getCanaelledCount(userId,startTime, endTime);
        return num;
//        return 0;
    }

    /**
     * 获取每个时间段剩余预约次数
     *
     * @param time
     * @return 每个时间段剩余预约次数
     */
    @Override
    public List<Map<String,String>> getCountByTime(Long time) {
        // 将毫秒数转换为LocalDateTime对象
        LocalDateTime lat= LocalDateTimeUtil.of(time);
        // 获取当天开始时间
        LocalDateTime startTime=lat.toLocalDate().atStartOfDay();
        // 获取当天结束时间
        LocalDateTime endTime=startTime.plusDays(1);

        //在线程中获取UserId
        Long userId;
        userId= UserThreadLocal.getUserId();
        Reservation reservation = reservationMapper.selectReservationById(userId);
        List<Map<String,String>> list=reservationMapper.getCountByTime(startTime,endTime);
        return list;
//        return null;
    }

    /**
     * 取消预约
     * @param id
     */
    @Override
    public void putCancel(Long id) {
        int result = reservationMapper.updateCancelById(id);
    }
}
